package com.vm.osm.business.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vm.osm.business.common.ErrCode;
import com.vm.osm.business.common.exception.BusinessException;
import com.vm.osm.business.dao.OsmKpiItemValueMapper;
import com.vm.osm.business.dao.OsmKpiMapper;
import com.vm.osm.business.dto.req.checkPlan.ReqAddKpi;
import com.vm.osm.business.dto.req.checkPlan.ReqUpdateKpi;
import com.vm.osm.business.dto.rsp.checkPlan.KpiItem;
import com.vm.osm.business.dto.rsp.checkPlan.RspKpiPage;
import com.vm.osm.business.dto.rsp.failOrder.FailOrderPage;
import com.vm.osm.business.entity.OsmKpi;
import com.vm.osm.business.entity.OsmKpiItemValue;
import com.vm.osm.business.service.IKpiService;
import com.vm.osm.business.util.UserDataUtil;
import com.vm.osm.business.util.UuidUtil;
import com.vma.eams.spring.boot.log.LogCollector;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by chenzui on 2018/2/9.
 */
@Service
public class KpiServiceImpl implements IKpiService {

    @Autowired
    private OsmKpiMapper osmKpiMapper;
    @Autowired
    private OsmKpiItemValueMapper osmKpiItemValueMapper;

    @Override
    public void addKpiInfo(ReqAddKpi reqAddKpi,String enterpriseId) {
        Date date = new Date();
        String kpiId = UuidUtil.getRandomUuid();
        String version = UuidUtil.getRandomUuid();
        OsmKpi osmKpi = new OsmKpi();
        osmKpi.setStatus(new Byte("1"));
        osmKpi.setCreateTime(new Date());
        osmKpi.setDeviceType(reqAddKpi.getDevice_type());
        osmKpi.setKpiName(reqAddKpi.getKpi_name());
        osmKpi.setSourceType(reqAddKpi.getSource_type());
        osmKpi.setUpdateTime(date);
        osmKpi.setCreateTime(date);
        osmKpi.setId(kpiId);
        osmKpi.setTypeName(reqAddKpi.getType_name());
        osmKpi.setEnterpriseId(enterpriseId);
        osmKpi.setVersion(version);
        osmKpiMapper.insertSelective(osmKpi);

        List<String> items = reqAddKpi.getItems();
        if (items != null && items.size() != 0) {
            for (String value : items) {
                OsmKpiItemValue itemValue = new OsmKpiItemValue();
                itemValue.setCreateTime(date);
                itemValue.setUpdateTime(date);
                itemValue.setKpiId(kpiId);
                itemValue.setKpiValue(value);
                itemValue.setStatus(new Byte("1"));
                itemValue.setId(UuidUtil.getRandomUuid());
                itemValue.setKpiVersion(osmKpi.getVersion());
                osmKpiItemValueMapper.insert(itemValue);
            }
        }

        LogCollector.INSTANCE.put("指标名称", osmKpi.getKpiName());
    }

    @Override
    public List<KpiItem> getKpiByCond(String deviceType, String enterpriseId,String kpiName) {
        List<KpiItem> kpiItems = osmKpiMapper.getKpiByCond(deviceType,enterpriseId,kpiName);

        for (KpiItem ki:kpiItems) {
            List<String> values = osmKpiMapper.getKpiValuesById(ki.getKpi_id(),ki.getKpi_version());
            ki.setItems(values);
        }

        return kpiItems;
    }

    @Override
    public RspKpiPage getPage(String deviceType, String enterpriseId, String kpiName,Integer pageNum,Integer pageSize) {
        RspKpiPage rspKpiPage = new RspKpiPage();
        Page page = PageHelper.startPage(pageNum,pageSize,true);
        List<KpiItem> kpiItems = osmKpiMapper.getKpiByCond(deviceType,enterpriseId,kpiName);
        for (KpiItem ki:kpiItems) {
            List<String> values = osmKpiMapper.getKpiValuesById(ki.getKpi_id(),ki.getKpi_version());
            ki.setItems(values);
        }
        rspKpiPage.setTotal_num(page.getTotal());
        rspKpiPage.setPage_size(page.getPageSize());
        rspKpiPage.setPage_num(page.getPageNum());
        rspKpiPage.setData_list(kpiItems);
        return rspKpiPage;
    }

    @Override
    public void updateKpi(ReqUpdateKpi reqUpdateKpi, HttpServletRequest request) {
        Date date = new Date();
        OsmKpi osmKpi = osmKpiMapper.selectByPrimaryKey(reqUpdateKpi.getKpi_id());
        if (osmKpi == null){
            throw new BusinessException(ErrCode.DATA_NOT_FOUND.toString());
        }else {
            osmKpiMapper.update(osmKpi);
        }
//        osmKpi.setDeviceType(reqUpdateKpi.getDevice_type());
//        osmKpi.setKpiName(reqUpdateKpi.getKpi_name());
//        osmKpi.setSourceType(reqUpdateKpi.getSource_type());
//        osmKpi.setUpdateTime(new Date());
//
//        List<String> items = reqUpdateKpi.getItems();
//        if(!items.isEmpty()){
//            // TODO: 2018/2/9 删除旧的离散值，添加新的 离散值
//            osmKpiItemValueMapper.deleteByKpiId(reqUpdateKpi.getKpi_id());
//
//            for (String value:items) {
//                OsmKpiItemValue itemValue = new OsmKpiItemValue();
//                itemValue.setCreateTime(date);
//                itemValue.setUpdateTime(date);
//                itemValue.setKpiId(reqUpdateKpi.getKpi_id());
//                itemValue.setKpiValue(value);
//                itemValue.setStatus(new Byte("1"));
//                itemValue.setId(UuidUtil.getRandomUuid());
//                itemValue.setKpiVersion(osmKpi.getVersion());
//                osmKpiItemValueMapper.insert(itemValue);
//            }
//        }
//
//        osmKpiMapper.updateOsmKpi(osmKpi);

        String kpiId = UuidUtil.getRandomUuid();
        String version = UuidUtil.getRandomUuid();
        OsmKpi newOsmKpi = new OsmKpi();
        newOsmKpi.setStatus(new Byte("1"));
        newOsmKpi.setCreateTime(new Date());
        newOsmKpi.setDeviceType(osmKpi.getDeviceType());
        newOsmKpi.setKpiName(reqUpdateKpi.getKpi_name());
        newOsmKpi.setSourceType(reqUpdateKpi.getSource_type());
        newOsmKpi.setUpdateTime(date);
        newOsmKpi.setCreateTime(date);
        newOsmKpi.setId(kpiId);
        newOsmKpi.setEnterpriseId(UserDataUtil.getUserSelectedEnterpriseId(request));
        newOsmKpi.setVersion(version);
        osmKpiMapper.insertSelective(newOsmKpi);

        List<String> items = reqUpdateKpi.getItems();
        if (items != null && items.size() != 0) {
            for (String value : items) {
                OsmKpiItemValue itemValue = new OsmKpiItemValue();
                itemValue.setCreateTime(date);
                itemValue.setUpdateTime(date);
                itemValue.setKpiId(kpiId);
                itemValue.setKpiValue(value);
                itemValue.setStatus(new Byte("1"));
                itemValue.setId(UuidUtil.getRandomUuid());
                itemValue.setKpiVersion(osmKpi.getVersion());
                osmKpiItemValueMapper.insert(itemValue);
            }
        }

    }

    @Override
    public void delete(String id) {
        OsmKpi osmKpi = osmKpiMapper.selectByPrimaryKey(id);

        osmKpi.setStatus(new Byte("1"));

        osmKpiMapper.update(osmKpi);
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        return osmKpiMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(OsmKpi record) {
        return osmKpiMapper.insert(record);
    }

    @Override
    public int insertSelective(OsmKpi record) {
        return osmKpiMapper.insertSelective(record);
    }

    @Override
    public OsmKpi selectByPrimaryKey(String id) {
        return osmKpiMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(OsmKpi record) {
        return osmKpiMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(OsmKpi record) {
        return osmKpiMapper.updateByPrimaryKey(record);
    }
}
